****migration-related moments. Things need to be adjusted so that it's appropriate to 
***draw equally from each state

use "$temp/acs_master_kid_sample", clear

su move if !coll [fw=perwt]
local movencoll = `r(mean)'
su move if coll [fw=perwt]
local movecoll = `r(mean)'

*****HC differences across movers and non-movers
//collapse (mean) hc [fw = perwt], by(bpl move)
su hc [fw=perwt] if move & !coll 
local mean1 =`r(mean)'
su hc [fw=perwt] if !move & !coll  //difference of 0.236 to target!
local mean2 = `r(mean)'
local diffncoll = `mean1' - `mean2'
di "`diffncoll'"
 
su hc [fw=perwt] if move  & coll
local mean1 =`r(mean)'
su hc [fw=perwt] if !move & coll  //difference of 0.236 to target!
local mean2 = `r(mean)'
local diffcoll = `mean1' - `mean2'
di "`diffcoll'"

noi di as result "Mover-Stayer HC Difference: `diff'"

*****Inflow and Outflow Rates*****

***outlfow rates
use "$temp/acs_master_kid_sample", clear
collapse (mean) move [fweight = perwt], by(bpl)
drop bpl
export delimited "$dir/Model/moments/state_outflow_rates.csv", novarn replace

***inflow rates
use "$temp/acs_master_kid_sample", clear
keep if move
gen count = 1
collapse (sum) count [fw=perwt], by(statefips)
egen grandcount = total(count)
gen frac = count/grand
keep frac
export delimited "$dir/Model/moments/state_inflow_rates.csv", novarn replace

*****state out-migration rate mean, SD*****
use "$temp/acs_master_kid_sample", clear
gen stay = (move == 0)
collapse (mean) stay [fw = perwt], by(bpl)
su stay
local stay_mean = `r(mean)'
local stay_sd = `r(sd)'


*****generate migration matrix
use "$temp/acs_master_kid_sample", clear
levelsof statefips, clean local(states)
foreach fip in `states'{
    gen st_dum_`fip' = (statefips == `fip')
}

collapse (mean) st_dum_* [fw=perwt], by(bpl)
save "$temp/state_mig_matrix", replace
drop bpl
export delimited "$dir/Model/moments/migration_matrix.csv", replace novarn

*****generate ranking of most popular destinations*****
//simulation of 2,000 people from each state
use "$temp/state_mig_matrix", clear
ds st*
foreach var in `r(varlist)'{
    replace `var' = `var' * 2000
}
collapse (sum) st_dum*
gen i = 1
reshape long st_dum_, i(i) j(fips)
drop i
ren st count
ren fips statefips
merge 1:1 statefips using "$data/Crosswalks/state_fips_crosswalk", keep(match) nogen
sort count
save "$temp/destinations_simulated", replace

*****correlation between state receiving migrants and population size*****
use "$temp/state_mig_matrix", clear
reshape long st_dum_, i(bpl) j(statefips)

//generate proportions of migrants from each state i who end up in each state j
drop if bpl == statefips //drop stayers
collapse (sum) st_dum_, by(statefips) //total share of migrants absorbed by each state
ren statefips stfips
merge 1:1 stfips using "$temp/state_populations_dynamic", keep(match) nogen
corr st_dum_ pop_2000
//local diffncoll = 0.042980526
//local diffcoll = 0.090220951
local corrin   = `r(rho)' //correlation

use "$temp/state_mig_matrix", clear
reshape long st_dum_, i(bpl) j(statefips)

//generate proportions of migrants from each state i who end up in each state j
keep if bpl == statefips //drop stayers
collapse (sum) st_dum_, by(statefips) //total share of migrants absorbed by each state
ren statefips stfips
merge 1:1 stfips using "$temp/state_populations_dynamic", keep(match) nogen
corr st_dum_ pop_2000
local corrout   = `r(rho)' //correlation

*****share of moves to proximal states*****
use "$temp/state_prox_mat", clear
reshape long v, i(stfips) j(bpl)
ren stfips statefips
save "$temp/state_prox_mat_long", replace

//merge to stay migration rates
use "$temp/state_mig_matrix", clear
reshape long st_dum_, i(bpl) j(statefips)
merge 1:1 bpl statefips using "$temp/state_prox_mat_long", keep(match) nogen

//ease interpretation: multiply all by 2k
replace st_dum_ = st_dum_ * 2000
drop if bpl == statefips
ren v prox
collapse (sum) st_dum_, by(prox) //sum up all migrants broken down by whether move is proximal
su st if !prox
local val0 = `r(mean)'
su st if prox
local val1 = `r(mean)'
local prox = `val1' / (`val0' + `val1')
di "`prox'"


//add things to moments vector
use "$temp/moments", clear
count 
local new = `r(N)' + 9
set obs `new'
local line1 = `r(N)' + 1
local line2 = `r(N)' + 2
local line3 = `r(N)' + 3
local line4 = `r(N)' + 4
local line5 = `r(N)' + 5
local line6 = `r(N)' + 6
local line7 = `r(N)' + 7
local line8 = `r(N)' + 8
local line9 = `r(N)' + 9
replace moment = `movencoll' in `line1'
replace moment = `movecoll' in `line2'
replace moment = `diffncoll' in `line3'
replace moment = `diffcoll' in `line4'
replace moment = `stay_mean' in `line5'
replace moment = `stay_sd' in `line6'
replace moment = `corrin' in `line7'
replace moment = `corrout' in `line8'
replace moment = `prox' in `line9'
save "$temp/moments_withmig", replace



//end of do-file
